package com.chhuang.icoolvideo;

import java.util.LinkedList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.chhuang.utils.Assert;


/**
 * 
 * 网免费观看的课程视频，可能下载要收费，那么用这个程序可以下载你想学的课程
 *
 */
public class App {
	/**
	 * 视频下载存放目录
	 */
	public static final String BASE_FILE_DIR = "E:/ExtJS视频教程/";
	/**
	 * 视频下载保存格式
	 */
	public static final String VIDEO_SUFFIX = ".mp4";
	/**
	 * ***地址
	 */
	public static final String BASE_URL = "http://****/";
	/**
	 * 课堂介绍页，用于抓取有视频列表，如下面地址是SpringMVC+Mybatis由浅入深全套视频教程
	 */
	public static final String ALBUM_URL = "album/show/87";
	/**
	 * 视频广告时长（单位：秒）
	 */
	public static final Integer AD_TIME = 30;
	/**
	 * 谷歌浏览器安装目录
	 */
	public static final String CHROME_APPLICATION = "C:/Users/CHHUANG/AppData/Local/Google/Chrome/Application/chrome.exe";
	/**
	 * chromedriver.exe谷歌驱动目录（注意：chrome的版本，都用最新的，下载chrome需要科学上网）
	 */
	public static final String CHROME_DRIVER = "C:/Users/CHHUANG/AppData/Local/Google/Chrome/Application/chromedriver.exe";
	
	//同时打开三个chrome进行下载
	private static List<Video> videos0 = null;
	private static List<Video> videos1 = null;
	private static List<Video> videos2 = null;
		
	public static void getVideo() {
		Connection conn = Jsoup.connect(BASE_URL+ALBUM_URL).timeout(60000);
		Document doc;
		try {
			doc = conn.get();
			Elements videoUls = doc.getElementsByClass("video-list");
			if(Assert.isValidCollection(videoUls)){
				Elements videoAs = videoUls.get(0).getElementsByTag("a");
				if(Assert.isValidCollection(videoAs)){
					if(videoAs.size()>0){
						videos0 = new LinkedList<Video>();
					}
					if(videoAs.size()>1){
						videos1 = new LinkedList<Video>();
					}
					if(videoAs.size()>2){
						videos2 = new LinkedList<Video>();
					}
					for(int i=0;i<videoAs.size();i++){
						Element videoA = videoAs.get(i);
						String url = videoA.attr("href");
						if(Assert.isValidString(url)){
							Video video = new Video();
							video.setPlayUrl(url);
							video.setVideoName(videoA.getElementsByClass("title").get(0).text());
							if(i%3==0){
								videos0.add(video);
							}else if(i%3==1){
								videos1.add(video);
							}else if(i%3==2){
								videos2.add(video);
							}
						}
					}
					Thread.sleep(1000);
					new Thread(new ParserVideoUrlThread(videos0)).start();
					Thread.sleep(1500);
					new Thread(new ParserVideoUrlThread(videos1)).start();
					Thread.sleep(2000);
					new Thread(new ParserVideoUrlThread(videos2)).start();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main( String[] args ){
    	App.getVideo();
    }
}
